<template>
	<view class="content">
		<view>
			<image src="../../static/bg.png" style="width: 100%;height: 398upx;position: relative;z-index: 1;"></image>
			<image src="../../static/logo.png" class="logo"></image>
			<!-- 后面做一键登录 -->
			<view class="regbox">
				<view class="regcontent">
					<view class="fsb" style="border-bottom: 1upx solid #EEEFF1;padding: 40upx 10upx 20upx;">
						<view class="flex aic" style="width: 100%;">
							<image src="../../static/l-1.png" style="width: 24upx;height: 34upx;margin-right: 10upx;"></image>
							<input style="margin-left: 20upx;" placeholder="请输入手机号" class="flex-1" v-model="phone" maxlength="20"/>
						</view>
					</view>
					<view class="fsb " style="border-bottom: 1upx solid #EEEFF1;padding: 40upx 10upx 20upx;">
						<view class="flex aic" style="width: 100%;">
							<image src="../../static/l-2.png" style="width: 34upx;height: 34upx;"></image>
							<input style="margin-left: 20upx;" type="password" placeholder="请输入密码" class="flex-1" v-model="password" maxlength="20"/>
						</view>
					</view>
					<button class="logbtn" :class="!phone || !password ?'defeat':'fill'"    @click="wisilogin()">登录</button>
					<view style="color: #1D84E8;font-size: 28upx;text-align: center;" class="flex aic">
						<view class="flex-1 leftcont" @click="wisireg">注册账号</view>
						<view class="flex-1" @click="forgetpsd()">忘记密码</view>
					</view>
				</view>
				<view style="margin-top: 140upx;">
					<view class="flex aic jc-center" style="color: #ddd;font-size: 26upx;">
						<text class="line"></text><text style="margin: 0 16upx;">第三方登录账号</text><text class="line"></text>
					</view>
					<view style="width: 440upx;margin: 40upx auto 0;" class="flex aic jc-center" >
						<!-- <image src="../../static/l-3.png" class="third"></image> -->
						
						<image @click="wxlogin" src="../../static/r-1.png" class="third"></image>
						
						<!-- <image src="../../static/l-5.png" class="third"></image> -->
					</view>
				</view>
			</view>
		</view>
	</view>
	</template>
	<script>
	import postAjax from '../../API/postAjax.js'
	export default {
		data() {
			return {
				phone:'',
				password:'',
				clientid:'1',
			}			
		},
		
		onLoad(options) {
			uni.hideNavigationBarLoading();
			var that=this
			// if (!that.clientid) { //如果获取的cid为空，说明客户端向推送服务器注册还未完成，可以使用setTimeout延时重试。
				
			// 	that.clientid = plus.push.getClientInfo().clientid				
			// }	
						
			
						
		},
		methods: {
			// 登录
			wisilogin(){
				let that = this
				if(that.phone == ''){
					uni.showToast({
						title: '请输入手机号',
						icon: 'none',
						duration: 1500
					});
					return
				}
				if(that.password == ''){
					uni.showToast({
						title: '请输入密码',
						icon: 'none',
						duration: 1500
					});
					return
				}
				
				// if (!that.clientid) { //如果获取的cid为空，说明客户端向推送服务器注册还未完成，可以使用setTimeout延时重试。
				// 	that.clientid = plus.push.getClientInfo().clientid				
				// }	
				
				let postData = {
					accounts:that.phone,
					pwd:that.password,
					channel_id:that.clientid,

				}
				postAjax('user/AppLogin',postData,function(data){
					let userInfo=data.data
					console.log(data)
					if(data.code == 0){
						uni.setStorageSync('userinfo', userInfo);
						if(userInfo.weixin_unionid){
							uni.showToast({	icon:'none',title:"登录成功"})
							uni.setStorageSync('token', userInfo.token);
							
							uni.setStorageSync('loginfo', postData);
							uni.setStorageSync('logintype', 1);
							uni.getSystemInfo({
							    success: function (res) {						       
									uni.setStorageSync('application_client_type', res.platform);
							    },
							})
							uni.switchTab({
								url:'../tab/index'
							})
						}else{
							uni.showToast({	icon:'none',title:"请去绑定微信"})
							
							setTimeout(function(){
								uni.navigateTo({
									url:'../user/setting/wxbind'
								})
							},1000) 
						}				

					}else{
						uni.showToast({	icon:'none',title:"登录失败"})

					}
				})
			},
			//注册
			wisireg(){
				uni.navigateTo({
					url:'register',
					animationDuration:300
				})
			},
			// 忘记密码
			forgetpsd(){
				uni.navigateTo({
					url:'forgetpsd',
					animationDuration:300
				})
			},
			wxlogin(){
				var that=this;
				
				uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
					
					// if (!that.clientid) { //如果获取的cid为空，说明客户端向推送服务器注册还未完成，可以使用setTimeout延时重试。
					// 	that.clientid = plus.push.getClientInfo().clientid				
					// }	
					let unionid=loginRes.authResult.unionid
					let openid=loginRes.authResult.openid
					
					if(!unionid){						
						that.wxlogin();
						return;						
					}
										
					postAjax('User/wxLogin',{
						unionid:unionid,						
						channel_id:that.clientid
						},function(data){
							if(data.code == 0){
								uni.setStorageSync('token', data.data.token);
								uni.setStorageSync('userinfo', data.data);
								uni.setStorageSync('logintype', 2);
								
								uni.getSystemInfo({
									success: function (res) {						       
										uni.setStorageSync('application_client_type', res.platform);
									},
								})																
								uni.switchTab({url:'../tab/index' })
							}else{
								uni.showToast({	icon:'none',title:data.msg})
							}					
						
							
					})
					
					
					
					
				  },
				  fail:function(res){
					  uni.showToast({
					    title:'获取微信授权失败',
					    icon:'none'
					  })
				  }
				})
			}
			
		}
	}
	
</script>

<style>
	.regbox{margin-top: -100upx;padding:0 30upx;position: relative;z-index: 10;}
	.regcontent{padding:50upx 50upx 110upx;background-color: #fff;border-radius: 20upx;box-shadow:0px 6px 13px rgba(29,132,232,0.2);}
	.logbtn{border-radius: 6upx;color:#fff;display:block;font-size: 30upx;border-radius: 4upx;width:456upx;height: 76upx;line-height: 76upx;text-align: center;padding: 0;margin: 100upx auto 40upx;}
	.logbtn::after{border: none;}
	.defeat{background: #ddd;}
	.third{width: 76upx;height: 76upx;}
	.line{width: 126upx;height: 2upx;background-color: #ddd;}
	.fill{background:linear-gradient(90deg,rgba(92,171,246,1) 0%,rgba(30,132,232,1) 100%);}
	.flex-1{flex:1;position: relative;}
	.leftcont::after{content: '';width: 2upx;height: 80%;background-color: #1D84E8;position: absolute;top:10%;right:0}
	.logo{position: absolute;width: 150upx;height: 30upx;top:150upx;left: 50%;margin-left: -75upx;z-index: 12;}
	/* .fillinp{padding-left: 16upx;font-size: 28upx;margin-left: 20upx;}
	.fillbox{padding:40upx 0 20upx;border-bottom: 2upx solid #eee;} */
</style>
